package com.kungfuhacking.wristbandpro.location.presenter;

import android.graphics.Bitmap;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.kungfuhacking.wristbandpro.base.bean.RequestBean;
import com.kungfuhacking.wristbandpro.base.presenter.BasePresenter;
import com.kungfuhacking.wristbandpro.location.bean.ShareBean;
import com.kungfuhacking.wristbandpro.location.view.IShareView;
import com.kungfuhacking.wristbandpro.login.activity.LoginActivity;
import com.kungfuhacking.wristbandpro.net.BaseNoTObserver;
import com.kungfuhacking.wristbandpro.net.RetrofitHttpUtil;
import com.kungfuhacking.wristbandpro.utils.Constant;
import com.kungfuhacking.wristbandpro.utils.LogUtil;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import io.reactivex.disposables.Disposable;

/**
 * Created by Aaron on 17/5/9.
 */

public class ISharePresenter implements BasePresenter {

    private IShareView iShareView;
    private String TAG = getClass().getSimpleName();

    public ISharePresenter(IShareView iShareView) {
        this.iShareView = iShareView;
    }

    /**
     * 获取邀请码
     */
    public void getInviteKey() {

        JSONObject j = new JSONObject();
        j.put("token", iShareView.getToken());
        j.put("id", iShareView.getSp().getString(Constant.CURRENTOLDID, ""));

        RetrofitHttpUtil.getInstance().getInviteKey(new BaseNoTObserver<ShareBean>() {
            @Override
            public void onHandleSuccess(ShareBean sharebean) {
                iShareView.hideLoading();
                if (TextUtils.isEmpty(sharebean.getInviteKey()))
                    iShareView.showErr("邀请码为空");
                else
                    iShareView.setShareCode("邀请码:" + sharebean.getInviteKey());
            }

            @Override
            public void onHandleError(String message) {
                iShareView.hideLoading();
                iShareView.showErr(message);
            }

            @Override
            public void onReLogin(String message) {
                iShareView.showErr(message);
                iShareView.hideLoading();
                iShareView.nextActivityWithSingleTask(LoginActivity.class);
            }

            @Override
            public void onSubscribe(Disposable d) {

                compositeDisposable.add(d);
                iShareView.showLoading("加载中...");
            }
        }, new RequestBean().setData(j.toString()));

    }

    /**
     * 分享
     * @param platform 分享的平台
     * @param bitmap 分享的图片
     */
    public void share(String platform, Bitmap bitmap) {

        Platform.ShareParams psp = new Platform.ShareParams();
        psp.setTitle("邀请码");

        psp.setText("[黄手环]  " +
                iShareView.getSp().getString(Constant.CURRENTOLDNAME, "") + "的邀请码为:  " +
                iShareView.getShareCode() + "  " + "下载APP 请前往 http://www.kungfuhacking.com");//邀请码

        psp.setImageUrl("http://liaoning.sinaimg.cn/2014/1111/U10435P1195DT20141111220802.jpg");
//        psp.setImagePath("http://liaoning.sinaimg.cn/2014/1111/U10435P1195DT20141111220802.jpg");
        psp.setImageData(bitmap);
//        psp.setUrl(url);
        psp.setShareType(Wechat.SHARE_TEXT);
        Platform weChat = ShareSDK.getPlatform(platform);
        weChat.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                iShareView.showErr("分享完成");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {
                LogUtil.d(TAG, "分享失败==" + throwable.getMessage());
                iShareView.showErr("分享失败");
            }

            @Override
            public void onCancel(Platform platform, int i) {
                iShareView.showErr("取消分享");
            }

        });
        weChat.share(psp);
    }


    @Override
    public void clearRequest() {
        compositeDisposable.clear();
    }
}
